<template>
    <fieldset>
        <legend>登入页</legend>
        用户名：
        <input type="text" v-model="username">
        <br>
        密码： <input type="password" v-model="password" @keydown.enter="login">
        <button @click="login">登入</button>
    </fieldset>
</template>
<script setup>
import { ref } from "vue"
import * as api from "../../api/users"
import { useRouter, onBeforeRouteLeave, onBeforeRouteUpdate, useRoute, useLink } from "vue-router"

//获取输入框中的用户名和密码
let username = ref("")
let password = ref("")

let router = useRouter()

let login = () => {
    api.loginApi(username.value, password.value)
        .then(res => {
        //    console.log(res);
           if(res.data.state){
            sessionStorage.setItem('api-token', res.data.token)
            //跳转到首页
            router.push('/')
           }else {
                alert(res.data.msg)
            }

        })
}

</script>